﻿<?php
///////////////////////////////////////////////////////////////////////////

require_once 'lib/abstract_controls_screen.php';

///////////////////////////////////////////////////////////////////////////

class SiolSetupScreen extends AbstractControlsScreen
{
	const ID = 'setup';
	private $known_lang;

	private $siol_plugin_setup_current_code;
	private $siol_plugin_setup_new_code;
	private $siol_plugin_setup_new_code_confirmation;
	private $siol_plugin_setup_button_apply;
	private $siol_plugin_setup_button_cancel;
	private $siol_plugin_setup_change_code;
	private $siol_plugin_setup_yes;
	private $siol_plugin_setup_no;
	private $siol_plugin_setup_show_in_main_screen;
	private $siol_plugin_show_epg_details;
	private $siol_plugin_setup_parent_code;
	private $siol_plugin_setup_parent_code_button_edit;
	private $siol_plugin_wrong_code_title;
	
    ///////////////////////////////////////////////////////////////////////

	public function __construct()
	{
		parent::__construct(self::ID);
    
    $this->siol_plugin_setup_current_code = 'Trenutna koda:';
    $this->siol_plugin_setup_new_code = 'Nova koda:';
    $this->siol_plugin_setup_new_code_confirmation = 'Potrditev:';
    $this->siol_plugin_setup_button_apply = 'Shrani';
    $this->siol_plugin_setup_button_cancel = 'Prekliči';
    $this->siol_plugin_setup_change_code = 'Spremeni kodo za zaščitene kanale';
    $this->siol_plugin_setup_yes = 'Da';
    $this->siol_plugin_setup_no = 'Ne';
    $this->siol_plugin_setup_show_in_main_screen = 'Prikaži TV v glavnem oknu:';
    $this->siol_plugin_show_epg_details = 'Prikaži opis TV programa';
    $this->siol_plugin_setup_parent_code = 'Koda za zaščitene kanale:';
    $this->siol_plugin_setup_parent_code_button_edit = 'Uredi...';
    $this->siol_plugin_wrong_code_title = 'Napačna koda'; 
    
		if (T::t('siol_plugin_setup_yes') == '%tr%siol_plugin_setup_yes')
		{
			$known_lang = false;
		}
		else
		{
			$known_lang = true;
		}
		
		if ($known_lang == true)
		{
			$this->siol_plugin_setup_current_code = T::t('siol_plugin_setup_current_code');
			$this->siol_plugin_setup_new_code = T::t('siol_plugin_setup_new_code');
			$this->siol_plugin_setup_new_code_confirmation = T::t('siol_plugin_setup_new_code_confirmation');
			$this->siol_plugin_setup_button_apply = T::t('siol_plugin_setup_button_apply');
			$this->siol_plugin_setup_button_cancel = T::t('siol_plugin_setup_button_cancel');
			$this->siol_plugin_setup_change_code = T::t('siol_plugin_setup_change_code');
			$this->siol_plugin_setup_yes = T::t('siol_plugin_setup_yes');
			$this->siol_plugin_setup_no = T::t('siol_plugin_setup_no');
			$this->siol_plugin_setup_show_in_main_screen = T::t('siol_plugin_setup_show_in_main_screen');
			$this->siol_plugin_show_epg_details = T::t('siol_plugin_show_epg_details');
			$this->siol_plugin_setup_parent_code = T::t('siol_plugin_setup_parent_code');
			$this->siol_plugin_setup_parent_code_button_edit = T::t('siol_plugin_setup_parent_code_button_edit');
			$this->siol_plugin_wrong_code_title = T::t('siol_plugin_wrong_code_title');
		}

		
	}
	
	private function do_get_parent_code_defs()
	{
		$defs = array();

		$this->add_text_field($defs,
			'current_parent_code', $this->siol_plugin_setup_current_code,
			'', false, false, false, true, 500);

        $this->add_text_field($defs,
            'new_parent_code', $this->siol_plugin_setup_new_code,
            '', false, false, false, true, 500);

        $this->add_text_field($defs,
            'new_parent_code_copy', $this->siol_plugin_setup_new_code_confirmation,
            '', false, false, false, true, 500);
			
		$this->add_vgap($defs, 50);
		
        $this->add_button($defs, 'apply_parent_code', null, $this->siol_plugin_setup_button_apply, 300);

        $this->add_vgap($defs, -3);

        $this->add_close_dialog_button($defs, $this->siol_plugin_setup_button_cancel, 300);
			
		return $defs;
	}
	
	private function do_get_parent_code_actions()
  {
			
		return ActionFactory::show_dialog($this->siol_plugin_setup_change_code, $this->do_get_parent_code_defs(), true);
  }

	public function do_get_control_defs(&$plugin_cookies)
	{
		$defs = array();

		$ShowSiolPluginInTv = isset($plugin_cookies->ShowSiolPluginInTv) ?
					$plugin_cookies->ShowSiolPluginInTv : 'yes';
		$SiolPluginShowEpgDetails = isset($plugin_cookies->SiolPluginShowEpgDetails) ?
					$plugin_cookies->SiolPluginShowEpgDetails : 'no';


		$show_ops = array();
		$show_ops['yes'] = $this->siol_plugin_setup_yes;
		$show_ops['no'] = $this->siol_plugin_setup_no;

	
		$this->add_combobox($defs,
					'ShowSiolPluginInTv', $this->siol_plugin_setup_show_in_main_screen,
					$ShowSiolPluginInTv, $show_ops, 0, true);
	
		$this->add_combobox($defs,
					'SiolPluginShowEpgDetails', $this->siol_plugin_show_epg_details,
					$SiolPluginShowEpgDetails, $show_ops, 0, true);
		
	/*
		$this->add_button($defs, 'edit_parent_code', T::t('siol_plugin_setup_parent_code'),
                T::t('siol_plugin_setup_parent_code_button_edit', 200));
*/
		$this->add_button($defs, 'edit_parent_code', $this->siol_plugin_setup_parent_code,
                $this->siol_plugin_setup_parent_code_button_edit, 200);
    return $defs;
	}

	public function get_control_defs(MediaURL $media_url, &$plugin_cookies)
	{
		return $this->do_get_control_defs($plugin_cookies);
	}

	public function handle_user_input(&$user_input, &$plugin_cookies)
	{
		hd_print('Setup: handle_user_input:');
		foreach ($user_input as $key => $value)
		{
			hd_print("  $key => $value");
		}
		$need_close_dialog = false;
			$need_reset_controls = false;
		$post_action = null;
		if ($user_input->action_type === 'apply')
		{
			$control_id = $user_input->control_id;
					//$new_value = $user_input->{$control_id};
			if ($control_id === 'edit_parent_code')
			{
				return $this->do_get_parent_code_actions();
			}
			else if ($control_id === 'apply_parent_code')
			{
				$SiolPluginParentCode = isset($plugin_cookies->SiolPluginParentCode) ?
				$plugin_cookies->SiolPluginParentCode : '';
			
				if ($user_input->current_parent_code == $SiolPluginParentCode &&
						$user_input->new_parent_code == $user_input->new_parent_code_copy)
				{
					$plugin_cookies->SiolPluginParentCode = $user_input->new_parent_code;
				}
				else
				{
					$post_action = ActionFactory::show_error(false, $this->siol_plugin_wrong_code_title);
					$need_reset_controls = true;
				}

				$need_close_dialog = true;
				$need_reset_controls = true;
			}
		
		}

    if ($user_input->action_type === 'confirm')
    {
			$control_id = $user_input->control_id;
			$new_value = $user_input->{$control_id};
			hd_print("Setup: changing $control_id value to $new_value");

			if ($control_id === 'ShowSiolPluginInTv')
			{
				$plugin_cookies->ShowSiolPluginInTv = $new_value;
			}
			else if ($control_id === 'SiolPluginShowEpgDetails')
			{
				$plugin_cookies->SiolPluginShowEpgDetails = $new_value;
			}
			else if ($control_id === 'new_parent_code')
			{
				$plugin_cookies->SiolPluginParentCode = $new_value;
			}
			$need_reset_controls = true;
    }

		if ($need_reset_controls)
		{
			$defs = $this->do_get_control_defs($plugin_cookies);

			$reset_controls_action = ActionFactory::reset_controls($defs, $post_action);

      if ($need_close_dialog)
      {
				return ActionFactory::close_dialog_and_run($reset_controls_action);
			}

			return $reset_controls_action;
		}

		return null;
	}
}

///////////////////////////////////////////////////////////////////////////
?>
